<script type="text/javascript">
$(document).ready(function(){
	$("#btn_add_role,#btn_remove_role,#update_choosen_value").click(function(){
		var user = $("#choosen_user").val();		
		if(user=='')
		{
			alert("Please Select User !");
			return false;
		}
		else
			return true;	
	});
});
</script>
<?php 
$list_user = get_users();
$list_link_admin = db_list_link_admin();
//print_r($list_link_admin);

if(isset($_POST['choosen_user']))
	$_SESSION['choosen_user'] = $_POST['choosen_user'];

if(isset($_POST) && $_POST['btn_add_role'] != null)
{
	$_SESSION['my_add_role'] = $_POST['list_post_type'];
	//print_r($_SESSION['my_add_role']);
}
else if(isset($_POST) && $_POST['btn_remove_role'] != null)
{
	$current_choosen = $_POST['list_choosen'];
	$myArray = array();
	if($_SESSION['my_add_role'])
	{
		foreach ($_SESSION['my_add_role'] as $value) 
		{
			if(!db_check_exists_value_in_array($current_choosen, $value))
				$myArray[] = $value;
		}
	}
	$_SESSION['my_add_role'] = $myArray;
}
else if(isset($_POST) && $_POST['update_choosen_value'] != null)
{
	$newvalue = '';
	if($_POST['list_choosen'])
	{
		$newvalue = implode(",", $_POST['list_choosen']);
	}
	$options_key = 'mysetting_user_role'.$_POST['choosen_user'];
	update_option($options_key, $newvalue);	
	
	echo '<p style="color: red;"> Update Success ! </p>';
}

?>


<!-- Cập nhật tùy chỉnh trang support_online -->                     
<div id="index_page" class="index_page"> 
  <h3>Config Role For user</h3>
   <form action="" method="post" id="user_index_page" >
   	<p>
   		<label for"select_user"> Select User: </label>
   		<select name="choosen_user" id="choosen_user" onchange="this.form.submit()">
   			<option value="">Choosen User</option>
   			<?php 
   			foreach ($list_user as $value) 
   			{
   				if($value->user_login != "admin")
   				{
   					if(isset($_SESSION['choosen_user']) && $_SESSION['choosen_user'] != null && $_SESSION['choosen_user'] == $value->ID)
   					{
   					?>
	   					<option selected="selected" value="<?php echo $value->ID;?>"><?php echo $value->user_login;?></option>
	   				<?php 	
   					}   					
   					else 
   					{
   					?>
	   					<option value="<?php echo $value->ID;?>"><?php echo $value->user_login;?></option>
	   				<?php 	
   					}
   				}
   			}
   			?>
   		</select> 
   		<label><b>Current Role: </b> <i><?php echo get_option('mysetting_user_role'.$_POST['choosen_user']);?></i></label>
   	</p>
   <p>
   		<ul>
   			<li style="width: 38%; margin-right: 30px; float: left;">    			
   			<table>
   				<tr>
   					<td style="font-size: 16px;"><b>List Category</b></td>
   					<td style="font-size: 16px;"><b>Choosen</b></td>
   				</tr>
   				
   				<tr>
   					<td>
   						<select name="list_post_type[]" multiple="multiple" size="10">
			   				<?php 
			   				
			   				foreach ($list_link_admin as $key=>$value) 
			   				{
			   				?>
				   				<option value="<?php echo $key;?>"><?php echo $key;?></option>	
			   				<?php 
			   				
			   				}
			   				?>
   						</select>
   					</td>
   					<td>
   						<input type="submit" name="btn_add_role" id="btn_add_role" value="Add To">
   					</td>
   				</tr>
   			</table>
   			</li>
   			
   			<!-- - -->
   			<li style="width: 38%; float: left;">    			
   			<table >
   				<tr>
   					<td style="font-size: 16px;"><b>User Choosen</b></td>
   					<td style="font-size: 16px;"><b>Action</b></td>
   				</tr>
   				<tr>
   					<td>
   						<select name="list_choosen[]" multiple="multiple" size="10">
			   				<?php 
			   				if($_SESSION['my_add_role'])
			   				{
				   				foreach ($_SESSION['my_add_role'] as $value) 
				   				{
				   				?>
					   				<option selected="selected" value="<?php echo $value;?>"><?php echo $value;?></option>	
				   				<?php 
				   				
				   				}
			   				}
			   				?>
   						</select>
   					</td>
   					<td>
   						<input type="submit" name="btn_remove_role" id="btn_remove_role" value="Remove">
   					</td>
   				</tr>
   			</table>
   			</li> 
   			<li style="float: left;">
   				<input type="submit" name="update_choosen_value" id="update_choosen_value" value="Update Choosen">
   			</li>  			
   		</ul>
   </p>
   </form>		           
</div>